home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Visual Basic 5.0 (2nd Edition) / Hardcore Visual Basic 5.0 - Second Edition (1997)(Microsoft Press).iso / Code / TPalette.frm < prev    next >
Text File  |  1997-06-14  |  11KB  |  384 lines

  1. VERSION 5.00
  2. Begin VB.Form FTestPalette 
  3.    AutoRedraw      =   -1  'True
  4.    Caption         =   "Pal (and I don't mean Buddy)"
  5.    ClientHeight    =   5475
  6.    ClientLeft      =   1995
  7.    ClientTop       =   1935
  8.    ClientWidth     =   6975
  9.    BeginProperty Font 
  10.       Name            =   "MS Sans Serif"
  11.       Size            =   8.25
  12.       Charset         =   0
  13.       Weight          =   700
  14.       Underline       =   0   'False
  15.       Italic          =   0   'False
  16.       Strikethrough   =   0   'False
  17.    EndProperty
  18.    ForeColor       =   &H80000008&
  19.    Icon            =   "TPalette.frx":0000
  20.    LinkTopic       =   "Form1"
  21.    PaletteMode     =   2  'Custom
  22.    Picture         =   "TPalette.frx":0CFA
  23.    ScaleHeight     =   5475
  24.    ScaleWidth      =   6975
  25.    Begin VB.HScrollBar hs 
  26.       Height          =   204
  27.       LargeChange     =   20
  28.       Left            =   1992
  29.       Max             =   20
  30.       Min             =   500
  31.       SmallChange     =   30
  32.       TabIndex        =   16
  33.       Top             =   984
  34.       Value           =   500
  35.       Width           =   852
  36.    End
  37.    Begin VB.OptionButton optPal 
  38.       Caption         =   "Picture"
  39.       Height          =   192
  40.       Index           =   1
  41.       Left            =   180
  42.       TabIndex        =   15
  43.       Top             =   720
  44.       Width           =   972
  45.    End
  46.    Begin VB.OptionButton optPal 
  47.       Caption         =   "Form"
  48.       Height          =   192
  49.       Index           =   0
  50.       Left            =   180
  51.       TabIndex        =   14
  52.       Top             =   540
  53.       Value           =   -1  'True
  54.       Width           =   972
  55.    End
  56.    Begin VB.CommandButton cmdNew 
  57.       Caption         =   "New Bitmap..."
  58.       Height          =   396
  59.       Left            =   1800
  60.       TabIndex        =   13
  61.       Top             =   504
  62.       Width           =   1308
  63.    End
  64.    Begin VB.TextBox txtTotal 
  65.       Enabled         =   0   'False
  66.       Height          =   285
  67.       Left            =   2700
  68.       MaxLength       =   3
  69.       TabIndex        =   12
  70.       Top             =   1716
  71.       Width           =   396
  72.    End
  73.    Begin VB.PictureBox pbPal 
  74.       Align           =   1  'Align Top
  75.       AutoRedraw      =   -1  'True
  76.       Height          =   360
  77.       Left            =   0
  78.       ScaleHeight     =   300
  79.       ScaleWidth      =   6915
  80.       TabIndex        =   9
  81.       Top             =   0
  82.       Width           =   6975
  83.    End
  84.    Begin VB.CommandButton cmdAcmeAnimate 
  85.       BackColor       =   &H8000000C&
  86.       Caption         =   "&< >"
  87.       Height          =   375
  88.       Index           =   3
  89.       Left            =   1680
  90.       TabIndex        =   8
  91.       Top             =   1296
  92.       Width           =   696
  93.    End
  94.    Begin VB.PictureBox pbBitmap 
  95.       AutoSize        =   -1  'True
  96.       Height          =   3960
  97.       Left            =   192
  98.       Picture         =   "TPalette.frx":BCD3C
  99.       ScaleHeight     =   3900
  100.       ScaleWidth      =   3600
  101.       TabIndex        =   7
  102.       Top             =   2076
  103.       Visible         =   0   'False
  104.       Width           =   3660
  105.    End
  106.    Begin VB.CommandButton cmdAcmeAnimate 
  107.       BackColor       =   &H8000000C&
  108.       Caption         =   "&Right"
  109.       Height          =   375
  110.       Index           =   1
  111.       Left            =   2412
  112.       TabIndex        =   6
  113.       Top             =   1296
  114.       Width           =   696
  115.    End
  116.    Begin VB.CommandButton cmdAcmeAnimate 
  117.       BackColor       =   &H8000000C&
  118.       Caption         =   "&> <"
  119.       Height          =   375
  120.       Index           =   2
  121.       Left            =   936
  122.       TabIndex        =   5
  123.       Top             =   1296
  124.       Width           =   696
  125.    End
  126.    Begin VB.TextBox txtTo 
  127.       Height          =   285
  128.       Left            =   1716
  129.       MaxLength       =   3
  130.       TabIndex        =   3
  131.       Top             =   1716
  132.       Width           =   396
  133.    End
  134.    Begin VB.TextBox txtFrom 
  135.       Height          =   285
  136.       Left            =   732
  137.       MaxLength       =   3
  138.       TabIndex        =   2
  139.       Text            =   "0"
  140.       Top             =   1704
  141.       Width           =   396
  142.    End
  143.    Begin VB.Timer tmrAnimate 
  144.       Left            =   10464
  145.       Top             =   96
  146.    End
  147.    Begin VB.CheckBox chkContinuous 
  148.       Caption         =   "Conti&nuous"
  149.       Height          =   255
  150.       Left            =   216
  151.       TabIndex        =   1
  152.       Top             =   960
  153.       Width           =   1344
  154.    End
  155.    Begin VB.CommandButton cmdAcmeAnimate 
  156.       Caption         =   "&Left"
  157.       Height          =   375
  158.       Index           =   0
  159.       Left            =   204
  160.       TabIndex        =   0
  161.       Top             =   1296
  162.       Width           =   696
  163.    End
  164.    Begin VB.Label lbl 
  165.       BackStyle       =   0  'Transparent
  166.       Caption         =   "S"
  167.       Height          =   216
  168.       Index           =   4
  169.       Left            =   1824
  170.       TabIndex        =   18
  171.       Top             =   972
  172.       Width           =   156
  173.    End
  174.    Begin VB.Label lbl 
  175.       BackStyle       =   0  'Transparent
  176.       Caption         =   "F"
  177.       Height          =   216
  178.       Index           =   3
  179.       Left            =   2892
  180.       TabIndex        =   17
  181.       Top             =   972
  182.       Width           =   156
  183.    End
  184.    Begin VB.Label lbl 
  185.       BackStyle       =   0  'Transparent
  186.       Caption         =   "&To:"
  187.       ForeColor       =   &H00000000&
  188.       Height          =   288
  189.       Index           =   2
  190.       Left            =   1212
  191.       TabIndex        =   11
  192.       Top             =   1704
  193.       Width           =   504
  194.    End
  195.    Begin VB.Label lbl 
  196.       BackStyle       =   0  'Transparent
  197.       Caption         =   "Total:"
  198.       ForeColor       =   &H00000000&
  199.       Height          =   288
  200.       Index           =   1
  201.       Left            =   2196
  202.       TabIndex        =   10
  203.       Top             =   1716
  204.       Width           =   504
  205.    End
  206.    Begin VB.Label lbl 
  207.       BackStyle       =   0  'Transparent
  208.       Caption         =   "&From:"
  209.       ForeColor       =   &H00000000&
  210.       Height          =   288
  211.       Index           =   0
  212.       Left            =   216
  213.       TabIndex        =   4
  214.       Top             =   1704
  215.       Width           =   504
  216.    End
  217. End
  218. Attribute VB_Name = "FTestPalette"
  219. Attribute VB_GlobalNameSpace = False
  220. Attribute VB_Creatable = False
  221. Attribute VB_PredeclaredId = True
  222. Attribute VB_Exposed = False
  223. Option Explicit
  224.  
  225. Private fOnPicture As Boolean
  226. Private pal As New CPalette
  227. Private cPal As Long
  228. Private aColors() As OLE_COLOR
  229. Private ecd As ECycleDirection
  230. Private iFormTo As Long, iFormFrom As Long
  231. Private iPicTo As Long, iPicFrom As Long
  232.  
  233. Private Sub Form_Load()
  234.  
  235.     Show
  236.     
  237.     Dim xPixels As Long, yPixels As Long
  238.     xPixels = Screen.Width / Screen.TwipsPerPixelX
  239.     yPixels = Screen.Height / Screen.TwipsPerPixelY
  240.     ' Use the largest size we can get away with
  241.     If xPixels <= 640 Or yPixels <= 480 Then
  242.         Width = 630 * Screen.TwipsPerPixelX
  243.         Height = 470 * Screen.TwipsPerPixelY
  244.     ElseIf xPixels < 800 Or yPixels < 600 Then
  245.         Width = 790 * Screen.TwipsPerPixelX
  246.         Height = 590 * Screen.TwipsPerPixelY
  247.     ElseIf xPixels <= 1024 Or yPixels <= 768 Then
  248.         Width = 1000 * Screen.TwipsPerPixelX
  249.         Height = 750 * Screen.TwipsPerPixelY
  250.     Else
  251.         Width = 1032 * Screen.TwipsPerPixelX
  252.         Height = 778 * Screen.TwipsPerPixelY
  253.     End If
  254.     
  255.     ' Initialize exclusions
  256.     iFormFrom = 0
  257.     iFormTo = 233
  258.     iPicFrom = 10
  259.     iPicTo = 236
  260.     
  261.     ' Fake button clicks to initialize
  262.     optPal_Click -fOnPicture
  263.     chkContinuous_Click
  264.         
  265. End Sub
  266.  
  267. Private Sub Form_Unload(Cancel As Integer)
  268.     pal.Destroy
  269. End Sub
  270.  
  271. Private Sub hs_Change()
  272.     tmrAnimate.Interval = hs.Value
  273. End Sub
  274.  
  275. Private Sub optPal_Click(Index As Integer)
  276.     Select Case Index
  277.     Case 0  ' Form
  278.         Palette = Picture
  279.         pbBitmap.Visible = False
  280.         DrawPalette pbPal, Picture.hPal
  281.         ' Create the palette and initialize the color array
  282.         cPal = pal.Create(Picture.hPal, hWnd, aColors, iFormFrom, iFormTo)
  283.         txtTotal = cPal
  284.         txtTo = iFormTo
  285.         txtFrom = iFormFrom
  286.         fOnPicture = False
  287.         
  288.     Case 1  ' Picture
  289.         pbBitmap.Visible = True
  290.         Palette = pbBitmap.Picture
  291.         DrawPalette pbPal, pbBitmap.Picture.hPal
  292.         ' Create the palette and initialize the color array
  293.         cPal = pal.Create(pbBitmap.Picture.hPal, pbBitmap.hWnd, _
  294.                           aColors, iPicFrom, iPicTo)
  295.         txtTotal = cPal
  296.         If iPicTo = -1 Then iPicTo = cPal
  297.         txtTo = iPicTo
  298.         txtFrom = iPicFrom
  299.         fOnPicture = True
  300.         
  301.     End Select
  302. End Sub
  303.  
  304. Private Sub chkContinuous_Click()
  305.     If chkContinuous.Value = vbChecked Then
  306.         tmrAnimate.Interval = 154
  307.         tmrAnimate.Enabled = True
  308.     Else
  309.         tmrAnimate.Enabled = False
  310.     End If
  311. End Sub
  312.  
  313. Private Sub cmdNew_Click()
  314.     Dim opfile As New COpenPictureFile, fTimerOn As Boolean
  315.     fTimerOn = tmrAnimate.Enabled
  316.     tmrAnimate.Enabled = False
  317.     With opfile
  318.         .InitDir = WindowsDir
  319.         .FilterType = efpBitmap
  320.         .Load Left + (Width / 4), Top + (Height / 4)
  321.         If .filename <> sEmpty Then
  322.             pbBitmap.Picture = LoadPicture(.filename)
  323.         End If
  324.     End With
  325.     iPicFrom = 0
  326.     iPicTo = -1
  327.     If pbBitmap.Picture.hPal <> hNull Then
  328.         optPal_Click 1
  329.         optPal(1).Value = True
  330.     Else
  331.         MsgBox "Bitmap does not have palette"
  332.     End If
  333.     tmrAnimate.Enabled = fTimerOn
  334. End Sub
  335.  
  336. Private Sub txtFrom_LostFocus()
  337.     If fOnPicture Then
  338.         iPicFrom = CLng(txtFrom)
  339.     Else
  340.         iFormFrom = CLng(txtFrom)
  341.     End If
  342.     optPal_Click -fOnPicture
  343. End Sub
  344.  
  345. Private Sub txtTo_LostFocus()
  346.     If fOnPicture Then
  347.         iPicTo = CLng(txtTo)
  348.     Else
  349.         iFormTo = CLng(txtTo)
  350.     End If
  351.     optPal_Click -fOnPicture
  352. End Sub
  353.  
  354. Private Sub cmdAcmeAnimate_Click(Index As Integer)
  355.     RotatePaletteArray aColors, Index
  356.     pal.ModifyPalette aColors
  357.     DrawPalette pbPal, pal.Handle
  358.     ecd = Index
  359. End Sub
  360.  
  361. Private Sub tmrAnimate_Timer()
  362.     Call cmdAcmeAnimate_Click(CInt(ecd))
  363. End Sub
  364.  
  365. Private Sub txtFrom_KeyPress(KeyAscii As Integer)
  366.     Select Case KeyAscii
  367.         Case 48 To 57, 8
  368.         Case Else
  369.             Beep
  370.             KeyAscii = 0
  371.     End Select
  372. End Sub
  373.  
  374. Private Sub txtTo_KeyPress(KeyAscii As Integer)
  375.     Select Case KeyAscii
  376.         Case 48 To 57, 8
  377.         Case Else
  378.             Beep
  379.             KeyAscii = 0
  380.     End Select
  381. End Sub
  382.  
  383.  
  384.